--- /dev/null
+github: BYVoid
steps:
- uses: actions/checkout@v4
- uses: bazelbuild/setup-bazelisk@v3
+ - name: Set up Python
+ uses: actions/setup-python@v5
+ with:
+ python-version: "3.12"
- run: bazel build //:opencc
- - run: bazel test --test_output=all //src/... //data/... //test/...
+ - run: bazel test --test_output=all //src/... //data/... //test/... //python/...
+.bazelversion
.npmignore
.gitignore
CMakeLists.txt
/src/*TestBase.cpp
/doc
/data/scheme
+/data/scripts
/deps/google-benchmark
-/deps/gtest*
+/deps/googletest*
/deps/pybind*
/deps/tclap*
/bazel-*
+load("@rules_cc//cc:defs.bzl", "cc_library")
+load("@rules_python//python:py_library.bzl", "py_library")
+
package(default_visibility = ["//visibility:public"])
cc_library(
"//src:opencc",
],
)
+
+py_library(
+ name = "py_opencc",
+ deps = [
+ "//python/opencc",
+ ],
+)
set (PACKAGE_BUGREPORT https://github.com/BYVoid/OpenCC/issues)
set (OPENCC_VERSION_MAJOR 1)
set (OPENCC_VERSION_MINOR 1)
-set (OPENCC_VERSION_REVISION 8)
+set (OPENCC_VERSION_REVISION 9)
if (CMAKE_BUILD_TYPE MATCHES Debug)
set (version_suffix .Debug)
module(
name = "opencc",
- version = "1.1.8",
+ version = "1.1.9",
compatibility_level = 1,
)
bazel_dep(name = "darts-clone", version = "0.32")
-bazel_dep(name = "googletest", version = "1.15.0")
+bazel_dep(name = "googletest", version = "1.15.0", dev_dependency = True)
bazel_dep(name = "marisa-trie", version = "0.2.6")
+bazel_dep(name = "platforms", version = "0.0.10")
+bazel_dep(name = "pybind11_bazel", version = "2.12.0")
bazel_dep(name = "rapidjson", version = "1.1.0")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_python", version = "0.34.0")
bazel_dep(name = "tclap", version = "1.2.5")
+
+
+python = use_extension("@rules_python//python/extensions:python.bzl", "python")
+python.toolchain(
+ python_version = "3.12",
+)
+pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
+pip.parse(
+ hub_name = "pip",
+ python_version = "3.12",
+ requirements_lock = "//python/tests:requirements_lock.txt",
+)
+use_repo(pip, "pip")
},
"recordedRepoMappingEntries": []
}
+ },
+ "@@pybind11_bazel~//:internal_configure.bzl%internal_configure_extension": {
+ "general": {
+ "bzlTransitiveDigest": "+F47SE20NlARCHVGbd4r7kkjg4OA0eCJcOd5fqKq4fQ=",
+ "usagesDigest": "iH2lKTfsNEpn2MqtGpBNwJrxbb2C7DiYmh/XuKgDtr8=",
+ "recordedFileInputs": {
+ "@@pybind11_bazel~//MODULE.bazel": "e6f4c20442eaa7c90d7190d8dc539d0ab422f95c65a57cc59562170c58ae3d34"
+ },
+ "recordedDirentsInputs": {},
+ "envVariables": {},
+ "generatedRepoSpecs": {
+ "pybind11": {
+ "bzlFile": "@@bazel_tools//tools/build_defs/repo:http.bzl",
+ "ruleClassName": "http_archive",
+ "attributes": {
+ "build_file": "@@pybind11_bazel~//:pybind11-BUILD.bazel",
+ "strip_prefix": "pybind11-2.12.0",
+ "urls": [
+ "https://github.com/pybind/pybind11/archive/v2.12.0.zip"
+ ]
+ }
+ }
+ },
+ "recordedRepoMappingEntries": [
+ [
+ "pybind11_bazel~",
+ "bazel_tools",
+ "bazel_tools"
+ ]
+ ]
+ }
}
}
}
## Installation 安裝
-See [Download](https://github.com/BYVoid/OpenCC/wiki/Download).
+### Package Managers 包管理器
+
+* [Debian](https://tracker.debian.org/pkg/opencc)
+* [Ubuntu](https://launchpad.net/ubuntu/+source/opencc)
+* [Fedora](https://packages.fedoraproject.org/pkgs/opencc/opencc/)
+* [Arch Linux](https://archlinux.org/packages/extra/x86_64/opencc/)
+* [Mac](https://formulae.brew.sh/formula/opencc)
+* [Bazel](https://registry.bazel.build/modules/opencc)
+* [Node.js](https://npmjs.org/package/opencc)
+* [Python](https://pypi.org/project/OpenCC/)
+
+### Prebuilt 預編譯
+
+* Windows (x86_64): [Latest build](https://ci.appveyor.com/api/projects/Carbo/opencc/artifacts/OpenCC.zip?branch=master&job=Environment:%20nodejs_version=none;%20Platform:%20x64)
+* Windows (x86): [Latest build](https://ci.appveyor.com/api/projects/Carbo/opencc/artifacts/OpenCC.zip?branch=master&job=Environment:%20nodejs_version=none;%20Platform:%20x86)
## Usage 使用
### Python
-[PyPI](https://pypi.org/project/OpenCC/) `pip install opencc` (Windows, Linux, Mac)
+`pip install opencc` (Windows, Linux, Mac)
```python
import opencc
}
```
+[Full example with Bazel](https://github.com/BYVoid/opencc-bazel-example)
+
### C
```c
-#!/usr/bin/env python
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
-#!/usr/bin/env python
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import codecs
-#!/usr/bin/env python
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
-#!/usr/bin/env python
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
-#!/usr/bin/env python
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import glob
import sys
{
"variables": {
- "opencc_version": "1.1.8"
+ "opencc_version": "1.1.9"
},
"target_defaults": {
"defines": [
{
"name": "opencc",
- "version": "1.1.8",
+ "version": "1.1.9",
"description": "Conversion between Traditional and Simplified Chinese",
"author": "Carbo Kuo <byvoid@byvoid.com>",
"license": "Apache-2.0",
--- /dev/null
+load("@rules_python//python:py_library.bzl", "py_library")
+
+package(default_visibility = ["//visibility:public"])
+
+py_library(
+ name = "opencc",
+ srcs = ["__init__.py"],
+ data = [
+ "//data/config",
+ "//data/dictionary:binary_dictionaries",
+ "//data/dictionary:text_dictionaries",
+ ],
+ imports = [".."],
+ deps = ["//src:py_opencc"],
+)
import os
import sys
-from opencc.clib import opencc_clib
+try:
+ import opencc_clib
+except ImportError:
+ from opencc.clib import opencc_clib
__all__ = ['OpenCC', 'CONFIGS', '__version__']
__version__ = opencc_clib.__version__
-_thisdir = os.path.dirname(os.path.abspath(__file__))
-_opencc_share_dir = os.path.join(_thisdir, 'clib', 'share', 'opencc')
+_this_dir = os.path.dirname(os.path.abspath(__file__))
+_opencc_share_dir = os.path.join(_this_dir, 'clib', 'share', 'opencc')
+_opencc_rootdir = os.path.abspath(os.path.join(_this_dir, '..', '..'))
+_opencc_configdir = os.path.join(_opencc_rootdir, 'data', 'config')
if sys.version_info.major == 2:
text_type = unicode # noqa
if os.path.isdir(_opencc_share_dir):
CONFIGS = [f for f in os.listdir(_opencc_share_dir) if f.endswith('.json')]
+elif os.path.isdir(_opencc_configdir):
+ CONFIGS = [f for f in os.listdir(_opencc_configdir) if f.endswith('.json')]
else:
CONFIGS = []
if not config.endswith('.json'):
config += '.json'
if not os.path.isfile(config):
- config = os.path.join(_opencc_share_dir, config)
+ config_under_share_dir = os.path.join(_opencc_share_dir, config)
+ if os.path.isfile(config_under_share_dir):
+ config = config_under_share_dir
super(OpenCC, self).__init__(config)
self.config = config
--- /dev/null
+load("@pip//:requirements.bzl", "requirement")
+load("@rules_python//python:py_test.bzl", "py_test")
+
+py_test(
+ name = "test_opencc",
+ srcs = ["test_opencc.py"],
+ data = [
+ "//test/testcases",
+ ],
+ imports = [".."],
+ deps = [
+ "//python/opencc",
+ requirement("pytest"),
+ requirement("exceptiongroup"),
+ ],
+)
--- /dev/null
+exceptiongroup==1.2.2
+iniconfig==2.0.0
+packaging==24.1
+pluggy==1.5.0
+pytest==8.3.2
from __future__ import unicode_literals
import os
+import pytest
+import sys
+
from glob import glob
_this_dir = os.path.dirname(os.path.abspath(__file__))
for text, ans in zip(intexts, anstexts):
assert converter.convert(text) == ans, \
'Failed to convert {} for {} -> {}'.format(pref, text, ans)
+
+
+if __name__ == "__main__":
+ sys.exit(pytest.main(sys.argv[1:]))
+load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
load("@rules_cc//cc:defs.bzl", "cc_library")
+load("@rules_python//python:py_library.bzl", "py_library")
package(default_visibility = ["//visibility:public"])
],
)
+pybind_extension(
+ name = "opencc_clib",
+ srcs = ["py_opencc.cpp"],
+ deps = [":opencc"],
+)
+
+py_library(
+ name = "py_opencc",
+ data = [":opencc_clib"],
+ imports = ["."],
+)
+
cc_library(
name = "segmentation",
srcs = ["Segmentation.cpp"],
Config::~Config() { delete reinterpret_cast<ConfigInternal*>(internal); }
+ConverterPtr Config::NewFromFile(const std::string& fileName) {
+ return NewFromFile(fileName, std::vector<std::string>{}, nullptr);
+}
+
ConverterPtr Config::NewFromFile(const std::string& fileName,
const std::vector<std::string>& paths,
const char* argv0) {
ConverterPtr NewFromString(const std::string& json,
const std::vector<std::string>& paths);
+ ConverterPtr NewFromFile(const std::string& fileName);
+
ConverterPtr NewFromFile(const std::string& fileName,
- const std::vector<std::string>& paths = {},
- const char* argv0 = nullptr);
+ const std::vector<std::string>& paths,
+ const char* argv0);
private:
void* internal;
paths_with_runfiles.push_back(
bazel_runfiles->Rlocation("_main/data/dictionary"));
return new InternalData(
- config.NewFromFile(configFileName, paths_with_runfiles));
+ config.NewFromFile(configFileName, paths_with_runfiles, argv0));
}
#endif
return new InternalData(config.NewFromFile(configFileName, paths, argv0));
outputFileName = Optional<std::string>(outputArg.getValue());
noFlush = true;
}
- converter = config.NewFromFile(configFileName, pathArg.getValue());
+ converter = config.NewFromFile(configFileName, pathArg.getValue(), argv[0]);
bool lineByLine = inputFileName.IsNull();
if (lineByLine) {
ConvertLineByLine();